﻿using ServiceStack.ServiceHost;
using ServiceStack.ServiceInterface;
using System;

namespace OneLoginPlugin
{
    [Route("/SAMLLogout", "GET")]
    public class SAMLLogout
    {

    }
    
    public class SAMLLogoutService : Service
    {
        public object Any(SAMLLogout request)
        {

            string userID = this.GetSession().UserAuthId;

            AccountSettings accountSettings = new AccountSettings(new ServiceStack.Configuration.AppSettings());

            AuthRequest req = new AuthRequest(accountSettings);

            string authRequest = req.GetLogoutRequest(AuthRequest.AuthRequestFormat.Base64, userID);

            UriBuilder uriBuilder = new UriBuilder(accountSettings.idp_sso_target_url);
            uriBuilder.Query.AddQueryParam(SAMLAuthProvider.SAML_REQUEST, authRequest);

            return this.Redirect(uriBuilder.ToString());
        }
    }
}
